package at.fhj.swd.pse.entities;

import java.util.List;

import javax.persistence.EntityManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.data.CommunityDAO;
import at.fhj.swd.pse.data.CommunityDAOImpl;
import static org.junit.Assert.*;

/**
 * 
 * @author Helmut Kopf
 *
 */
public class UTCommunityDAO
	extends JpaTestBase
{
	private final EntityManager m_em = getEntityManagerForPersistenceUnit("test");
	private CommunityDAO m_dao;
	
	@Before
    public void setUp()
    {
        m_dao = new CommunityDAOImpl(m_em);        
        
        begin();
    }   

    @After
    public void tearDown()
    {
        rollback();
    	//commit();
        closeEntityManager();
    }       
	
    @Test 
    public void testRemoveCommunity()
    {
		Community co = createCommunity();
			
		m_dao.addCommunity(co);
		
		Community co_t = m_dao.getCommunity(co.getID());
		assertNotNull(co_t);
		
		m_dao.removeCommunity(co_t);
		assertNull(m_dao.getCommunity(co.getID()));		
    }
	
	@Test
	public void testAddCommunity() 
	{
		Community co = createCommunity();
				
		m_dao.addCommunity(co);
		
		List<Community> communities = m_dao.getCommunities();
				
		assertTrue(communities.size() > 0);			
		assertNotNull(findCommunity(communities, co.getID()));
					
		Community co_t = m_dao.getCommunity(co.getID());
		assertNotNull(co_t);
		assertEquals(co.getName(), co_t.getName());
		assertEquals(co.getDescription(), co_t.getDescription());
		assertEquals(co.getTopic(), co_t.getTopic());
		
		compareUsers(co.getAdministractor(), co_t.getAdministractor());
		
		assertEquals(2, co_t.getMembers().size());
				
		for (User user : co.getMembers())
		{
			compareUsers(user, findUser(co_t.getMembers(), user.getId()));
		}
	}
	
    private Community createCommunity()
    {
		Community co = new Community();
		
		User admin = new User();
		admin.setFirstName("Community");
		admin.setLastName("Admin");
		admin.setAddress("Anyroad");
		admin.setCity("Nowhere");
		admin.setEmail("admin@community.com");
		
		User member1 = new User();
		member1.setFirstName("A");
		member1.setLastName("Member");
		member1.setAddress("Over");
		member1.setCity("TheRainbow");
		member1.setEmail("member1@community.com");
		
		User member2 = new User();
		member2.setFirstName("Another");
		member2.setLastName("MemberXX");
		member2.setAddress("Sillicon");
		member2.setCity("Valley");
		member2.setEmail("member2@community.com");		
				
		co.setName("TestCommunity");
		co.setDescription("A brief description of TestCommunity...");
		co.setTopic("The topic of this community");
		co.setAdministractor(admin);
		
		co.addMember(member1);
		co.addMember(member2);
		
		return co;
    }
    
	private Community findCommunity(List<Community> communities, int id)
	{
		for (Community community : communities)
		{
			if (community.getID() == id)
			{
				return community;
			}
		}
		
		return null;
	}    
	
	private User findUser(List<User> users, int id)
	{
		for (User user : users)
		{
			if (user.getId() == id)
			{
				return user;
			}
		}
		
		return null;
	}
	
	private void compareUsers(User usr1, User usr2)
	{
		if (usr1 != null)
		{
			assertNotNull(usr2);
			assertEquals(usr1.getFirstName(), usr2.getFirstName());
			assertEquals(usr1.getLastName(), usr2.getLastName());
			assertEquals(usr1.getAddress(), usr2.getAddress());
			assertEquals(usr1.getCity(), usr2.getCity());
			assertEquals(usr1.getEmail(), usr2.getEmail());
		}
		else
		{
			assertNull(usr2);
		}	
	}	
}
